package cn.edu.scau.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.scau.entity.Blog;
import cn.edu.scau.entity.User;
import cn.edu.scau.service.UserService;

@Controller
@RequestMapping(value="/User",method={RequestMethod.POST,RequestMethod.GET})
public class UserController {
	
	@Resource(name="userServiceImpl")
	private UserService userService;
	
	/**
	 * 注册
	 * @param user 待注册的用户类
	 * @param req
	 * @return 不同的返回结果
	 */
	@RequestMapping(value="/Register",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody String registerUser(User user,HttpServletRequest req){
		Integer result = userService.registerTheUser(user);
		if(result==1){
			System.out.println(1);
			return  "{" + '"' + "data" + '"' + ":" + '"' + 1 + '"' + "}";
		}else if(result==2){
			System.out.println(2);
			return "{" + '"' + "data" + '"' + ":" + '"' + 2 + '"' + "}";
		}else if(result==3){
			System.out.println(3);
			userService.sendEmail(user.getUserId());
			return "{" + '"' + "data" + '"' + ":" + '"' + 3 + '"' + "}";
		}else{
			System.out.println(4);
			return "{" + '"' + "data" + '"' + ":" + '"' + 4 + '"' + "}";
		}
	}
	/**
	 * 登录
	 * @param checkCode 用户输入的验证码
	 * @param req
	 * @param resp
	 * @return 不同的返回结果
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/Login",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody String loginUser(@RequestParam String checkCode,HttpServletRequest req,HttpServletResponse resp) throws UnsupportedEncodingException{
		String picCode = (String) req.getSession().getAttribute("picCode");
		return "{" + '"' + "data" + '"' + ":" + '"' + userService.loginTheUser(picCode, checkCode,req,resp).toString() + '"' + "}";	
	}
	
	/**
	 * 激活
	 * @param userId 用户id
	 * @return 不同的返回结果
	 */
	@RequestMapping(value="/UserBeActive",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody String userBeActive(@RequestParam Integer userId){
		return  "{" + '"' + "data" + '"' + ":" + '"' + userService.checkBeActive(userId).toString() + '"' + "}";
	}
	
	/**
	 * 产生激活码
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @throws IOException
	 */
	@RequestMapping(value="/ReducePicCode",method={RequestMethod.POST,RequestMethod.GET})
	public void reducePicCode(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException{
		userService.picCode(httpServletRequest, httpServletResponse);
	}
	
	/**
	 * 更新用户信息
	 * @param userId 用户Id
	 * @param updateUser 需要更新的用户类
	 * @param resp
	 * @return 不同的返回结果
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/UpdateUserMessage",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody String updateTheUserMessage(@RequestParam Integer userId,User updateUser,HttpServletResponse resp) throws UnsupportedEncodingException{
		return "{" + '"' + "data" + '"' + ":" + '"' + userService.updateTheUserMessage(userId, updateUser,resp) + '"' + "}";
	}
	/**
	 * 更改头像
	 * @param userHeadPicture 头像文件
	 * @param userId 用户id
 	 * @param req
	 * @return
	 */
	@RequestMapping(value="/ChangeTheUserHeadPicture",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody String changeTheUserHeadPicture(@RequestParam MultipartFile[] userHeadPicture,@RequestParam Integer userId,HttpServletRequest req,HttpServletResponse resp) {
		try {
			return "{" + '"' + "data" + '"' + ":" + '"' + userService.changeTheUserHeadPicture(userId, userHeadPicture, req,resp).toString() + '"' + "}";
		}catch (Throwable e) {
			return "{" + '"' + "data" + '"' + ":" + '"' + 4 + '"' + "}";
		}
	}
	/**
	 * 查找用户
	 * @param userId 用户id
	 * @return
	 */
	@RequestMapping(value="/FindUser",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody User findUser(@RequestParam Integer userId){
		return userService.findUser(userId);
	}
	/**
	 * 添加用户微博
	 * @param userId 用户Id
	 * @param files 文件
	 * @param blog 需要添加的微博类
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/AddUserBlog",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody String addUserBlog(@RequestParam Integer userId,@RequestParam MultipartFile[] files,Blog blog,HttpServletRequest req){
		
		try {
			List<Object> objects = userService.saveFiles(files, req);
			List<String> imagePaths = (List<String>) objects.get(0);
			String videoPaths = (String) objects.get(1);
			if(imagePaths!=null){
				blog.setImageAdresses(imagePaths);
			}
			if(videoPaths!=null){
				blog.setVideoAdress(videoPaths);
			}
			blog.setTime(new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").format(new Date().getTime()));
			userService.addUserBlog(userId, blog);
			return "{" + '"' + "data" + '"' + ":" + '"' + true + '"' + "}";
		}catch (Throwable e) {
			e.printStackTrace();
			// TODO Auto-generated catch block
			return "{" + '"' + "data" + '"' + ":" + '"' + false + '"' + "}";
		}
	}
	/**
	 * 添加收藏
	 * @param userId 用户id
	 * @param blogId 博客id
	 * @return
	 */
	@RequestMapping(value="/AddUserColBlog",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody String addUserColBlog(@RequestParam Integer userId,@RequestParam Integer blogId){
		return "{" + '"' + "data" + '"' + ":" + '"' + userService.addUserColBlog(userId, blogId) + '"' + "}";
	}
	/**
	 * 根据类型查找与用户有关的微博
	 * @param userId 用户id
	 * @param pageNo 页数
	 * @param type 类型
	 * @return
	 */
	@RequestMapping(value="/FindUserBlogByType",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody List<Blog> findUserBlogByType(@RequestParam Integer userId,@RequestParam Integer pageNo,@RequestParam Integer type){
		return userService.findUserBlogByType(userId, pageNo, type);
	}
	/**
	 * 根据类型查找与用户有关的微博的最大页数
	 * @param userId 用户id
	 * @param type 类型
	 * @return
	 */
	@RequestMapping(value="/GetBlogsMaxPageByType",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody String getBlogsMaxPageByType(@RequestParam Integer userId,@RequestParam Integer type){
		return "{" + '"' + "maxPage" + '"' + ":" + '"' + userService.getBlogsMaxPageByType(userId, type) + '"' + "}";
	}
	/**
	 * 根据类型删除与用户有关的微博
	 * @param userId 用户id
 	 * @param blogId 博客的id
 	 * @param type 类型
	 * @return
	 */
	@RequestMapping(value="/DeleteBlogByType",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody String deleteBlogByType(@RequestParam Integer userId,@RequestParam Integer blogId,@RequestParam Integer type){
		return "{" + '"' + "data" + '"' + ":" + '"' + userService.deleteBlogByType(userId, blogId, type) + '"' + "}";
	}
	
}
